<?php
namespace App\Service\ApiXcx;

use Illuminate\Support\Facades\DB;

class IncomeService extends BaseService
{

//    /**
//     * 可用列表
//     * @param array $pages【start，limit】
//     * @return array
//     */
//    public function getList($pages, $uid)
//    {
//        $start = ($pages['start'] - 1) * $pages['limit'];
//        $db = DB::table($this->dbUserIncome)
//            ->where('del', 0)
//            ->where('uid', $uid);
//        $total = $db->count();
//        $res = $db->skip($start)->take($pages['limit'])
//            ->orderBy('id', 'desc')
//            ->get()
//            ->map(function ($value){
//                return (array)$value;
//            })
//            ->toArray();
//        if (!count($res)) {
//            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
//        }
//        return array('code' => 0, 'msg' => 'success', 'data' => $res, 'total' => $total);
//    }

    /**
     * 可用列表
     * @param array $pages【start，limit】
     * @return array
     */
    public function getList($pages, $uid)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUserIncome)
            ->where('del', 0)
            ->where('uid', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(0, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'orderno' => $v['orderno'],
                'amount' => toNumber($v['amount']),
                'ordertime' => $v['ordertime'],
                'status' => $v['status'],
                'del' => $v['del'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, 'success', $arys, $total);
    }

//    /**
//     * 分销订单列表
//     * @param array $pages【start，limit】
//     * @return array
//     */
//    public function getOrderList($pages, $uid, $status)
//    {
//        $start = ($pages['start'] - 1) * $pages['limit'];
//        $db = DB::table($this->dbUserIncome)->where('del', 0)->where('uid', $uid);
//        if($status) {
//            $db->where('status', $status);
//        }
//        $total = $db->count();
//        $income_arr = $db->skip($start)->take($pages['limit'])
//            ->orderBy('id', 'desc')
//            ->get()
//            ->map(function ($value){
//                return (array)$value;
//            })
//            ->toArray();
//        if(!$income_arr) {
//            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
//        }
//        foreach ($income_arr as &$income) {
//            $order_info = (array)DB::table($this->dbOrder)->where('orderno', $income['orderno'])->where('del', 0)->first();
//            if (!$order_info) {
//                $income = [];
//                continue;
//            }
//            $order_sku_info = DB::table($this->dbOsku)
//                ->where('order_id', $order_info['id'])
//                ->where('del', 0)
//                ->get()
//                ->map(function ($value){
//                    return (array)$value;
//                })
//                ->toArray();
//            if(!$order_sku_info){
//                $income = [];
//                continue;
//            }
//            $order_user_info = (array)DB::table($this->dbUser)->where('id', $order_info['uid'])->where('del', 0)->first();
//            if(!$order_user_info){
//                $income = [];
//                continue;
//            }
//
//            //状态
//            $cancels = $this->orderCancels;
//            $status1s = $this->orderStatus1s;
//            $status2s = $this->orderStatus2s;
//            if ($order_info['iscancel'] > 1) {
//                $status_name = array_key_exists($order_info['iscancel'], $cancels) ? $cancels[$order_info['iscancel']] : '';
//            } else if ($order_info['delivery'] == 1) {
//                $status_name = array_key_exists($order_info['status'], $status1s) ? $status1s[$order_info['status']] : '';
//            } else {
//                $status_name = array_key_exists($order_info['status'], $status2s) ? $status2s[$order_info['status']] : '';
//            }
//
//            $img_list = [];
//            foreach ($order_sku_info as $sku) {
//                $img_list[] = $sku['gimg'];
//            }
//
//            $income = [
//                'uname' => $order_info['uname'],
//                'num' => $order_info['num'],
//                'price' => $order_info['price'],
//                'addtime' => $order_info['addtime'],
//                'status_name' => $status_name,
//                'avatarUrl' => $order_user_info['avatarUrl'],
//                'phone' => $order_user_info['phone'],
//                'img_list' => $img_list,
//                'commission_amount' => toNumber($income['amount']),
//            ];
//        }
//        if (!count($income_arr)) {
//            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
//        }
//        return array('code' => 0, 'msg' => 'success', 'data' => $income_arr, 'total' => $total);
//    }

    /**
     * 分销订单列表
     * @param array $pages【start，limit】
     * @return array
     */
    public function getOrderList($pages, $uid, $status)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbUserIncome)
            ->where('del', 0)
            ->where('uid', $uid);
        if($status) {
            $db->where('status', $status);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(0, '', [], 0);
        }
        $cancels = $this->orderCancels;
        $status1s = $this->orderStatus1s;
        $status2s = $this->orderStatus2s;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $orderOne = (array)DB::table($this->dbOrder)
                ->where('orderno', $v['orderno'])
                ->where('del', 0)
                ->first();
            $osOnes = $this->dbConn($this->dbOsku)
                ->where('order_id', $orderOne['id'])
                ->where('del', 0)
                ->get();
            $ouOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $orderOne['uid'])
                ->where('del', 0)
                ->first();
            $imgList = [];
            if (count($osOnes)) {
            }
            foreach ($osOnes as $vv) {
                $vv = (array) $vv;
                $gimg = '';
                if (substr($vv['gimg'], 0, 4) == 'http') {
                    $gimg = $vv['avatarUrl'];
                } else if (file_exists($vv['gimg'])) {
                    $gimg = env('DOMAIN') . '/' . $vv['gimg'];
                }
                $imgList[] = $gimg; //$vv['gimg'];
            }
            $status_name = '-';
            if (!empty($orderOne)) {
                if ($orderOne['iscancel'] > 1) {
                    $status_name = array_key_exists($orderOne['iscancel'], $cancels)
                        ? $cancels[$orderOne['iscancel']] : '';
                } else if ($orderOne['delivery'] == 1) {
                    $status_name = array_key_exists($orderOne['status'], $status1s)
                        ? $status1s[$orderOne['status']] : '';
                } else {
                    $status_name = array_key_exists($orderOne['status'], $status2s)
                        ? $status2s[$orderOne['status']] : '';
                }
            }
            $avatarUrl = '';
            if (!empty($ouOne)) {
                if (substr($ouOne['avatarUrl'], 0, 4) == 'http') {
                    $avatarUrl = $ouOne['avatarUrl'];
                } else if (file_exists($ouOne['avatarUrl'])) {
                    $avatarUrl = env('DOMAIN') . '/' . $ouOne['avatarUrl'];
                }
            }
            $arys[] = [
                'uname' => empty($orderOne) ? '-' : $orderOne['uname'],
                'num' => empty($orderOne) ? 0 : $orderOne['num'],
                'price' => empty($orderOne) ? 0 : toNumber($orderOne['price']),
                'addtime' => empty($orderOne) ? '' : $orderOne['addtime'],
                'status_name' => $status_name,
                'avatarUrl' => $avatarUrl,
                'phone' => empty($ouOne) ? '' : $ouOne['phone'],
                'img_list' => $imgList,
                'commission_amount' => toNumber($v['amount']),
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}